package org.jamin.lending.controller.manager;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jamin.lending.domain.BJUIAjaxResult;
import org.jamin.lending.domain.entity.SMSLog;
import org.jamin.lending.domain.entity.Template;
import org.jamin.lending.domain.form.manger.SMSConfigForm;
import org.jamin.lending.domain.form.manger.SMSLogForm;
import org.jamin.lending.domain.form.manger.SmsSuppliers;
import org.jamin.lending.domain.form.manger.TemplateForm;
import org.jamin.lending.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(value = "admin/message")
public class MessageManagerController {

    private Log log = LogFactory.getLog(getClass());

    @Autowired
    private MessageService messageService;

    @RequestMapping(value = "template")
    public String toTemplate(TemplateForm templateForm, ModelMap modelMap) {
        modelMap.addAttribute(templateForm);
        Page<Template> templates = this.messageService.findTemplate(templateForm);
        modelMap.addAttribute("templates", templates);
        return "manager/message/template";
    }

    @ResponseBody
    @RequestMapping(value = "reset_template")
    public BJUIAjaxResult resetTemplate(HttpServletRequest request) {
        String pathname = request.getRealPath("/config/mailTemplate.json");
        try {
            this.messageService.initMessageTemplate(pathname);
            return BJUIAjaxResult.createSuccess("重制成功");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return BJUIAjaxResult.createFail(e.getMessage());
        }
    }

    @RequestMapping(value = "template_edit", method = RequestMethod.GET)
    public String templateEdit(String code, ModelMap modelMap) {
        Template template = this.messageService.getTemplateOne(code);
        modelMap.addAttribute(template);
        return "manager/message/template_edit";
    }

    @ResponseBody
    @RequestMapping(value = "template_edit", method = RequestMethod.POST)
    public BJUIAjaxResult doTemplateEdit(Template template) {
        try {
            this.messageService.saveTemplate(template);
            BJUIAjaxResult ajaxResult = BJUIAjaxResult.createSuccess("模板编号" + template.getCode() + "修改成功");
            ajaxResult.setCloseCurrent("dialog");
            return ajaxResult;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return BJUIAjaxResult.createFail(e.getMessage());
        }
    }

    /**
     * 显示短信首页
     * @param smsLogForm
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "sms_index")
    public String smsIndex(SMSLogForm smsLogForm,ModelMap modelMap){
        Page<SMSLog> smsLogs = this.messageService.findSMSLogList(smsLogForm);
        modelMap.addAttribute("smsLogForm",smsLogForm);
        modelMap.addAttribute("smsLogs",smsLogs);
        return "manager/message/sms_index";
    }


    @RequestMapping(value = "sms_config",method = RequestMethod.GET)
    public String toSmsConfig(SMSConfigForm smsConfigForm,ModelMap modelMap){
        modelMap.addAttribute("smsConfigForm",smsConfigForm);
        modelMap.addAttribute("smsSuppliers", SmsSuppliers.values());
        return "manager/message/sms_config";
    }

}
